package com.tsingtree.base.consts;

import com.tsingtree.roses.core.util.ToolUtil;
import com.tsingtree.sys.core.constant.Const;
import com.tsingtree.sys.modular.system.config.Config;
import lombok.extern.slf4j.Slf4j;
import org.beetl.ext.fn.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import java.io.File;

/**
 * 系统常量的容器
 *
 * @author jagger
 */
@Slf4j
@Component
public class ConstantsContext {

    @Autowired
    private Config config;
    private final static String DEFAULT_PWD = "111111";
    private final static String PROJECT_NAME = "Sacoku";
    private final static String JWT_SECRET_KEY = "zsafmqadxqpgqvd9uysbb1g96qlvsjle";
    private final static Long JWT_SECRET_EXPIRE = 86400L;
    private final static String DEFAULT_AUTH_NAME = "Authorization";
    private static Boolean isTest;

    @PostConstruct
    public void init(){
        isTest = config.isTest;
    }

    public static String getSystemName() {
        return PROJECT_NAME;
    }

    public static String getDefaultPassword() {
        return DEFAULT_PWD;
    }


    public static String getReleaseVersion() {
        return ToolUtil.getRandomString(8);
    }
    /**
     * 获取系统发布的版本号（防止css和js的缓存）
     */
    public static String getCdn() {
        if(StringUtils.hasText(Const.OSS_CDN)){
            return Const.OSS_CDN;
        }
        return isTest? "http://127.0.0.1": Const.OSS_CDN;
    }

    /**
     * 获取文件上传路径(用于头像和富文本编辑器)
     */
    public static String getFileUploadPath() {
        return isTest? getWebAppsPath(): "/www";
    }

    private static String getWebAppsPath(){
        return System.getProperty("user.dir")  + File.separator + "tsing-base"
                + File.separator + "src" + File.separator + "main" + File.separator + "webapp" + File.separator;
    }


    public static String getJwtSecret() {
        return JWT_SECRET_KEY;
    }

    public static void main(String[] args) {
        System.out.println(ToolUtil.getRandomString(32));
    }

    public static Long getJwtSecretExpireSec() {
        return JWT_SECRET_EXPIRE;
    }

    /**
     * 获取token的header标识
     */
    public static String getTokenHeaderName() {
       return DEFAULT_AUTH_NAME;
    }


}
